#include "common.h"

static unsigned short read_buf[FLASH_PAGE_SIZE / 2];

unsigned char write_flash_page(unsigned int addr, unsigned short *data, unsigned int num)
{

    unsigned      re_write_cnt = 2;
    unsigned char ret          = 0;
    while (re_write_cnt--) {
        flash_unlock();
        flash_clear_flag();
        flash_erase_page(addr);
        flash_write_halfword(addr, data, num);
        flash_lock();
        flash_read_halfword(addr, read_buf, num);
        for (unsigned int i = 0; i < FLASH_PAGE_SIZE / 2; i++) {
            if (read_buf[i] != data[i]) {
                ret = 1;
                break;
            } else {
                ret = 0;
            }
        }

        if (0 == ret)
            break;
    }

    return ret;
}
